package web.mb;

import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import metier.entities.Etat;
import metier.entities.Pays;
import metier.entities.Ville;
import metier.sess.IEtatLocal;
import metier.sess.IPaysLocal;
import metier.sess.IVilleLocal;



@ManagedBean
@ViewScoped
public class CityMB {
	
	@EJB 
	private IVilleLocal metier;
	
	
	@EJB
	private IEtatLocal metiere;
	
	@EJB 
	private IPaysLocal meteirp;
	
	static private Integer Id;
	static private Integer IdS;
	static private Integer IdC;
	static private Ville ville=new Ville();
	private Set<Etat> listeEtat;
	
	
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public Integer getIdS() {
		return IdS;
	}
	public void setIdS(Integer idS) {
		IdS = idS;
	}
	public Integer getIdC() {
		return IdC;
	}
	public void setIdC(Integer idC) {
		IdC = idC;
	}
	public Ville getVille() {
		return ville;
	}
	public void setVille(Ville ville) {
		this.ville = ville;
	}
	
	
	public Set<Etat> getListeEtat() {
		return listeEtat;
	}
	public void setListeEtat(Set<Etat> listeEtat) {
		this.listeEtat = listeEtat;
	}
	
	public String Ajouter()
	{
		
		ville.setEtat(metiere.getEtat(IdS));
		metier.addVille(ville);
		return "ManageCity.xhtml";
	}
	
	public String edit(Integer Id)
	{
		this.Id=Id;
		Ville v=metier.getVille(Id);
		this.ville.setIntitule(v.getIntitule());
		this.ville.setStatut(v.getStatut());
    	IdS=v.getEtat().getIdEtat();
    	IdC=metiere.getEtat(IdS).getPays().getIdPays();
    	listeEtat=meteirp.getPays(IdC).getListEtat();
		return "EditCity.xhtml";
    
	}
	
	public String Modifier()
	{
		
		
		Ville v=metier.getVille(Id);
		v.setIntitule(ville.getIntitule());
		v.setStatut(ville.getStatut());
		v.setEtat(metiere.getEtat(IdS));
		metier.updateVille(v);
		ville=new Ville();
		Id=0;
		IdC=0;
		IdS=0;
		return "ManageCity.xhtml";
	}

	public String Supprimer(Integer Id)
	{
		
		metier.deleteVille(Id);
		return "succes";
		
	}
	
	
	public List<Ville> getListVille()
	{
		return metier.getAllVille();
	}

	
	public List<Pays> getListPays()
	{
		return meteirp.getAllPaysActive();
	}
	
	
	 public void onCountryChange() {
		 
	        if(IdC!=0)
	        {
	        	System.out.println("ici"+IdC);
	        	listeEtat=meteirp.getPays(IdC).getListEtat();
	        }
	        else
	        {
	        	System.out.println("iciNo");
	        	listeEtat=null;
	        }
	    }

}
